(function() {
    Nokia.FloatingMenu = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				autoRender: true,
				elementHoverClass: '',
				transitions: false,
				offsetTop: 0,
				offsetLeft: 0
			};

            instance.options = jQuery.extend(defaults, options);

            instance.items = instance.options.items;

            instance._super.apply(instance, [instance.options]);

            instance.registerData("floatingmenu");

            instance.registerVibrationOn([]);

            if (instance.options.autoRender) {
            	instance.render();
            }

        },

        _attachEvents: function() {
        	var instance = this;

        	jQuery(document.body).unbind('click.floatingmenu');
        	jQuery(document.body).bind('click.floatingmenu', function(event) {

        		if (event.target && event.target != instance.element[0]) {
        			instance.hideMenu();
        		}

        	});

        },

		create: function() {
			var instance = this,
				element = instance.element,
				offset = element.offset(),
				options = instance.options,
				Styles = instance.Styles;

			instance.wrapper = Nokia.dom.parseHTML('<div class="' + [Styles.floatingMenu, Styles.uiWidgetContent].join(' ') + '"></div>');
			instance.wrapper.css({
				top: offset.top + options.offsetTop,
				left: offset.left + options.offsetLeft
			}).hide();

			Nokia.dom.append(instance.element, instance.wrapper);

			Nokia.util.addClass(element[0], [Styles.uiClearfix, Styles.floatingMenuElement].join(' '));

			jQuery.each(instance.items, function(i, item) {
				var itemElement = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + Styles.floatingMenuItem + '">' + item.label + '</a>');
				itemElement.addClass(item.classname ? item.classname : Styles.uiStateDefault);

				itemElement.bind('focus.floatingmenu', function() {
					Nokia.util.addClass(this, Styles.uiStateHover);
				});

				itemElement.bind('blur.floatingmenu', function() {
					Nokia.util.removeClass(this, Styles.uiStateHover);
				});

				itemElement.hover(function() {
					itemElement.trigger('focus.floatingmenu');
				}, function() {
					itemElement.trigger('blur.floatingmenu');
				});

				itemElement.bind('click.floatingmenu', function(event) {
					// Trigger callback
					if (item.callback) {
						item.callback.apply(instance, [event]);
					}

					instance.hideMenu();

					event.stopPropagation();
				});

				Nokia.util.bindEnter(itemElement, function(e) {
					itemElement.trigger('click.floatingmenu');
				});

				Nokia.dom.append(instance.wrapper, itemElement);
			});

			instance.fireCallback('create');

		},

		destroy: function() {
			var instance = this;
			var Styles = instance.Styles;

			if (instance.wrapper) {
				instance.wrapper.remove();
			}

			if (instance.element) {
				instance.element.removeClass(Styles.floatingMenuElement);
			}

		},

		hideMenu: function() {
			var instance = this;

			instance.wrapper.hide();

			// Unmark element
			instance.element.removeClass(instance.options.elementHoverClass);

			// Trigger hide callback
			instance.fireCallback('hide', null);
		},

		render: function() {
			var instance = this;
			var Styles = instance.Styles;

			if (instance.options.element) {
				instance.element = jQuery( instance.options.element );

				if (!instance.element.hasClass(Styles.floatingMenuElement)) {
					instance._attachEvents();
	            	instance.create();
				}

			}

			return instance;
		},

		showMenu: function() {
			var instance = this;

			instance.wrapper.show();

			// Mark element
			instance.element.addClass(instance.options.elementHoverClass);

			// Trigger show callback
			instance.fireCallback('show', null);
		},

		toggle: function() {
			var instance = this;

			if (instance.wrapper.is(':visible')) {
				instance.hideMenu();
			} else {
				instance.showMenu();
			}

			// Trigger toggle callback
			instance.fireCallback('toggle', null);
		},

		Styles: {
			floatingMenu: 'nokia-floatingmenu',
			floatingMenuElement: 'nokia-floatingmenu-element',
			floatingMenuItem: 'nokia-floatingmenu-item',
			uiClearfix: 'ui-helper-clearfix',
			uiStateDefault: 'ui-state-default',
			uiStateActive: 'ui-state-active',
			uiStateHover: 'ui-state-hover',
			uiWidgetContent: 'ui-widget-content',
			nokiaHidden: 'nokia-hidden'
		}
    });
})();